                                  package com.yixiang.np.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yixiang.np.annotation.SystemControllerLog;
import com.yixiang.np.constant.GlobalConstant;
import com.yixiang.np.model.Json;
import com.yixiang.np.model.SessionInfo;
import com.yixiang.np.model.Tree;
import com.yixiang.np.model.po.Resource;
import com.yixiang.np.service.ResourceServiceI;

@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {

	@Autowired
	private ResourceServiceI resourceService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/admin/resource";
	}                                                                         

	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree(HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(GlobalConstant.SESSION_INFO);
		return resourceService.tree(sessionInfo);
	}

	@RequestMapping("/allTree")
	@ResponseBody
	public List<Tree> allTree(boolean flag) {//true获取全部资源,false只获取菜单资源
		return resourceService.allTree(flag);
	}

	@RequestMapping("/treeGrid")
	@ResponseBody
	public List<Resource> treeGrid() {
		return resourceService.treeGrid();
	}

	@RequestMapping("/get")
	@ResponseBody
	public Resource get(Long id) {
		return resourceService.get(id);
	}
	
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id) {
		Resource r = resourceService.get(id);
		request.setAttribute("resource", r);
		return "/admin/resourceEdit";
	}


	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Resource resource) throws InterruptedException {
		Json j = new Json();
		try {
			resourceService.edit(resource);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/delete")
	@ResponseBody
	@SystemControllerLog(module="系统管理",methods="资源管理-删除资源")
	public Json delete(Long id) {
		Json j = new Json();
		try {
			resourceService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/addPage")
	public String addPage() {
		return "/admin/resourceAdd";
	}

	@RequestMapping("/add")
	@ResponseBody
	@SystemControllerLog(module="系统管理",methods="资源管理-添加资源")
	public Json add(Resource resource) {
		Json j = new Json();
		try {
			resourceService.add(resource);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	//顶部菜单点击跳转
	@RequestMapping("/head/{pid}/{id}")
	public String head(@PathVariable Long pid, @PathVariable Long id,HttpServletRequest request, HttpSession session) {
		Resource resource = resourceService.get(id);
		List<Resource> list = (List<Resource>)session.getAttribute("userResource");
		boolean haveQx = false;
		for(Resource re : list){
			if(id.equals(re.getId())){
				haveQx = true;
			}
		}
		if(!haveQx){//判断是否有权限
			return "redirect:/np/nopermission";
		}
		String resourceFirst = "";
		String resourceSecond = "";
		String path = request.getServletContext().getContextPath();
		if(list != null){
			for(Resource r : list){
				if(r.getPid() == null){//一级菜单下所有的二级菜单
					if(pid != null && r.getId().equals(pid)){//第一级菜单
						resourceFirst += "<li class=\"nav1\" id=\"navActive\">"+r.getName()+"<img class=\"triangle\" id=\"appear\" src=\""+path+"/image/triangle.gif\"></li>";
					}else{
						resourceFirst += "<li class=\"nav1\">"+r.getName()+"<img class=\"triangle\" src=\""+path+"/image/triangle.gif\"></li>";
					}

					if(r.getId().equals(pid)){
						resourceSecond += "<ul class=\"navSec appear\" >";
					}else{
						resourceSecond += "<ul class=\"navSec\" >";
					}
					for(Resource s : list){
						if(s.getPid() != null && s.getPid().equals(r.getId())){
							if(id != null && s.getId().equals(id)){//第二级菜单
								resourceSecond += "<li><a class=\"visited\" href=\""+path+"/resource/head/"+r.getId()+"/"+s.getId()+"\">"+s.getName()+"</a></li>";
							}else{
								resourceSecond += "<li><a href=\""+path+"/resource/head/"+r.getId()+"/"+s.getId()+"\">"+s.getName()+"</a></li>";
							}
						}
					}
					resourceSecond += "</ul>";
				}
			}
		}
		session.setAttribute("resourceFirst", resourceFirst);
		session.setAttribute("resourceSecond", resourceSecond);
		return "redirect:" + resource.getUrl();
	}
}
